<?php PremiumPress_Header(); 

// DEFIND CONSTANT VALUES
$currency_symbol = get_option("currency_symbol");
$running_total=0;
$running_ship=0;
$extraSearch = "";
$checkbox = 1;
$rows_per_page =10;
if(!isset($_GET['p'])){ $c_page = 0; }else { $c_page = $_GET['p']*$rows_per_page; } 
?>


<?php if(isset($_GET['id']) && is_numeric($_GET['id']) ){ 

global $wpdb;

$order   = array("\r\n", "\n", "\r");
$replace = '<br />';
 

$SQL = "SELECT * FROM wp_shopperpress_orders LEFT JOIN $wpdb->users ON ($wpdb->users.ID = wp_shopperpress_orders.cus_id)  WHERE wp_shopperpress_orders.autoid = '".$_GET['id']."' GROUP BY order_id LIMIT 1"; 
 
$posts = mysql_query($SQL, $wpdb->dbh) or die(mysql_error().' on line: '.__LINE__);

while ($order = mysql_fetch_object($posts)) {

?>

<?php if(isset($_GET['u']) ){  ?>
<div style="border:1px solid green; padding:20px; margin-bottom:20px;  font-size:15px; background:#E6FFE6; font-weight:bold; color:#333333;">Order Details Updated </div>
<?php } ?>
<form method="post" name="ShopperPress" target="_self" <?php if($GLOBALS['sf'] ==1){ ?>onsubmit="UpgradeVIP();return false;"<?php } ?>>
<input name="saveorder" type="hidden" value="yes" />
<input type="hidden" name="autoid" value="<?php echo $order->autoid ?>">
<table style="margin-bottom: 20px;"></table>
<h3 class="title">Order ID: #<?php echo $order->order_id ?>  </h3>
<table class="maintable" style="background:white;">


	<tr class="mainrow">

		<td class="titledesc">Order ID</td>
		<td class="forminp">
			<input type="text" class="txt" name="order_id" value="<?php echo $order->order_id ?>" style="width:150px;"><br />
			<small>Date: <?php echo $order->order_date ?> <?php echo $order->order_time ?></small>
		</td>
	</tr>
 
	<tr class="mainrow">
		<td class="titledesc">Order Total</td>
		<td class="forminp">

			<input type="text" class="txt" name="order_total" value="<?php echo $order->order_total ?>" style="width:100px;"><br />
			<small>Without Shipping is: <?php echo get_option("currency_code").ShopperPressMoneyFormat($order->order_total-$order->order_shipping); ?></small>
		</td>
	</tr>
<?php if(PREMIUMPRESS_SYSTEM == "ShopperPress"){ ?>
	<tr class="mainrow">
		<td class="titledesc">Order Shipping</td>
		<td class="forminp">
			<input type="text" class="txt" name="order_shipping" value="<?php echo $order->order_shipping ?>" style="width:100px;">
		</td>

	</tr>
<?php } ?>
	<tr class="mainrow">
		<td class="titledesc">Order Description:</td>
		<td class="forminp">
			<textarea style="height:300px; width:500px;" name="order_data"><?php echo strip_tags(str_replace("<br/>","\r\n\r\n",$order->order_data)); ?></textarea>
		</td>
	</tr>

</table>


<?php if(PREMIUMPRESS_SYSTEM == "ShopperPress"){ ?>
 <table style="margin-bottom: 20px;"></table>
<h3 class="title">Delivery Address</h3>
<table class="maintable" style="background:white;">

	<tr class="mainrow">
		<td class="titledesc">Customer </td>
		<td class="forminp">
			<input type="text" class="txt" name="cus_id" value="<?php echo $order->cus_id ?>" style="width:100px;">
		</td>
	</tr>


 
	<tr class="mainrow">
		<td class="titledesc">Delivery Address</td>
		<td class="forminp">

			<textarea style="height:300px; width:500px;"  name="order_address"><?php echo strip_tags(str_replace("<p>","\r\n\r\n",$order->order_address)); ?></textarea>
		</td>
	</tr>
	<tr class="mainrow">
		<td class="titledesc">Delivery Country</td>
		<td class="forminp">
			<input type="text" class="txt" name="order_country" value="<?php echo $order->order_country ?>" style="width:500px;">
		</td>

	</tr>
	<tr class="mainrow">
		<td class="titledesc">Contact Phone</td>
		<td class="forminp">
			<input type="text" class="txt" name="order_phone" value="<?php echo $order->order_phone ?>" style="width:150px;">
		</td>
	</tr>
	<tr class="mainrow">

		<td class="titledesc">Contact Email</td>
		<td class="forminp">
			<input type="text" class="txt" name="order_email" value="<?php echo $order->order_email ?>" style="width:500px;">
		</td>
	</tr>
 
</table>
<?php } ?>
<div style=" background:#666; padding:15px; color:white; font-size:16px;">Order Status: <input name="order_status" type="radio" value="0" <?php if($order->order_status ==0){ echo "checked"; } ?>> Awaiting Payment   <input name="order_status" type="radio" value="3" <?php if($order->order_status ==3){ echo "checked"; } ?>> Paid & Completed </div>
<br />
<div style=" background:#666; padding:15px; color:white; font-size:16px;">Payment: <input name="order_status" type="radio" value="5" <?php if($order->order_status ==5){ echo "checked"; } ?>> Received  <input name="order_status" type="radio" value="7" <?php if($order->order_status ==7){ echo "checked"; } ?>> Pending <input name="order_status" type="radio" value="6" <?php if($order->order_status ==6){ echo "checked"; } ?>> Error/Failed <input name="order_status" type="radio" value="8" <?php if($order->order_status ==8){ echo "checked"; } ?>> Refunded</div> 
<br />

<p><input class="button" type="submit" value="Save changes" /></p>

</form>






<?php } }else{ ?>


<script>
function da(x){for(var j=1;j<=x;j++){box=eval("document.orderform.d"+j);box.checked=true;}}
function du(x){for(var j=1;j<=x;j++){box=eval("document.orderform.d"+j);box.checked=false;}}

</script>



<form method="post" name="SearchForm" target="_self" <?php if($GLOBALS['sf'] ==1){ ?>onsubmit="UpgradeVIP();return false;"<?php } ?>>
 <input name="searchorders" type="text" class="txt" style="font-size:18px; width:200px;" id="sb"><input name="" type="submit" style="font-size:18px; background:#a70303; color:white;" value="Search">
<input type="button" style="font-size:18px;" value="Reset" onclick="document.getElementById('sb').value=''; document.SearchForm.submit();">
</form>
<div id="premiumpress_box1" class="premiumpress_box premiumpress_box-100"><div class="premiumpress_boxin"><div class="header">
<h3><img src="<?php bloginfo('template_url'); ?>/images/premiumpress/h-ico/smiley.png" align="absmiddle"> Orders</h3>
<ul>
<li><a rel="premiumpress_tab1" href="#" class="active">Search Results</a></li>

</ul>
</div>
<div id="premiumpress_tab1" class="content">

<form class="plain" method="post" name="orderform" id="orderform">
<input type="hidden" name="deleteorders" value="1">
<fieldset>
<table cellspacing="0"><thead><tr>
<td class="tc"><input type="checkbox" id="data-1-check-all" name="data-1-check-all" value="true" onClick="da(10);return false;" /></td>
<td class="tc">Amount</td>
<th>Order ID / Status</th><td>Description</td><td class="tc">Date</td><td class="tc">Actions</td></tr></thead><tfoot><tr><td colspan="6">
<label>with selected do:
<select name="data-1-groupaction"><option value="delete">delete</option></select></label>
<input class="button altbutton" type="submit" value="OK" style="color:white;" />
</td></tr></tfoot><tbody>


<?php


if(isset($_POST['searchorders'])){
	$extraSearch = "WHERE (order_id LIKE '%".$_POST['searchorders']."%' OR order_email LIKE '%".$_POST['searchorders']
	."%' OR order_data LIKE '%".$_POST['searchorders']."%')";
}

$SQL = "SELECT count(*) AS total FROM wp_shopperpress_orders LEFT JOIN $wpdb->users ON ($wpdb->users.ID = wp_shopperpress_orders.cus_id)  ".$extraSearch." "; 
$results = mysql_query($SQL, $wpdb->dbh);
$values = mysql_fetch_assoc($results);
$num_rows = $values['total'];
$dd = ceil($num_rows/$rows_per_page);
$SQL = str_replace("count(*) AS total","*",$SQL). " GROUP BY order_id LIMIT ".$c_page.",".$rows_per_page;
$posts = mysql_query($SQL, $wpdb->dbh);
if ($posts && mysql_num_rows($posts) > 0) {
 
while ($order = mysql_fetch_object($posts)) {

	$totalPro =0;
	$totalQty =0;
	$OD = explode(",",$order->order_items);
	foreach($OD as $val){
		$val1 = explode("x",$val); 
		$totalPro ++;
		$totalQty += $val1[1];
	}

?>

<tr class="first">
<td class="tc">
<input type="checkbox" value="on" name="d<?php echo $checkbox; ?>" id="d<?php echo $checkbox; ?>"/>
<input type="hidden" name="d<?php echo $checkbox; ?>-id" value="<?php echo $order->autoid ?>">
</td>
<td class="tc"><span class="tag tag-gray"><?php echo $currency_symbol."".ShopperPressMoneyFormat($order->order_total-$order->order_shipping); $running_total += $order->order_total-$order->order_shipping; ?></span></td>
<th>
<a class="row-title" href="admin.php?page=orders&id=<?php echo $order->autoid ?>">  <?php echo $order->order_id ?></a><br />
<?php if($order->order_status ==0){ ?>
Awaiting Payment
<?php }elseif($order->order_status ==3){ ?>
Paid & Completed 
<?php }elseif($order->order_status ==5){ ?>
<b style="color:green;">Payment Received</b>
<?php }elseif($order->order_status ==6){ ?>
<b style="color:red;">Payment Failed</b>
<?php }elseif($order->order_status ==7){ ?>
<b style="color:blue;">Payment Pending</b>
<?php }elseif($order->order_status ==8){ ?>
<b>Payment Refunded</b>
<?php } ?>
</th>
<td>
<?php echo substr(strip_tags(str_replace("<br/>","\r\n\r\n",$order->order_data)),0,100); ?>..
<a href="admin.php?page=orders&id=<?php echo $order->autoid ?>" style="font-size:11px">full details</a> 
<small><?php echo $totalQty; ?> Item(s) - <?php if(strlen($order->user_nicename) > 1){ ?>Ordered By <a href="edit.php?author=<?php echo $order->cus_id ?>"><?php echo $order->user_nicename  ?></a><?php } ?> </small></p>

</td>
<td class="tc"><?php echo $order->order_date ?> <br /> <small>@ <?php echo $order->order_time ?></small></td>
<td class="tc">
<ul class="actions">
<li><a class="ico" href="admin.php?page=orders&id=<?php echo $order->autoid ?>" rel="permalink"><img src="<?php bloginfo('template_url'); ?>/images/premiumpress/led-ico/pencil.png" alt="edit" /></a></li>
<li><a class="ico" class='submitdelete' href='admin.php?page=orders&delo=<?php echo $order->autoid ?>' onclick="if ( confirm('Are you sure you want to delete this order?') ) { return true;}return false;"><img src="<?php bloginfo('template_url'); ?>/images/premiumpress/led-ico/delete.png" alt="delete" /></a></li>
</ul>
</td>
</tr>


</td>	</tr>
	</tbody>
<?php $checkbox++; } } ?>

</table>
<input type="hidden" name="totalorders" value="<?php echo $checkbox; ?>">
<div class="pagination">
<ul>
<li><a href="#">Showing <?php if($c_page ==0){ $c_page=1; } echo $c_page; ?> of <?php if($dd ==0){ echo "1"; }else{ echo $dd; }; ?></a></li>

<?php
$pc = $dd;
while($pc > 0){
$pnum = $pc; 
?>
<li><a href="admin.php?page=orders&p=<?php echo $pnum-1; ?>"><?php echo $pc; ?></a></li>
<?php $pc--; } ?>
</ul>
</div>
</div></div>

 
<div class="clearfix"></div> 

<div style="padding:15px; background:#666; color:white; font-size:16px; margin-top:15px;"> Order Total: <?php echo $currency_symbol.ShopperPressMoneyFormat($running_total); ?>  </div>
 
 
 
<a href="admin.php?page=orders&exportcsv=1">Export to CSV</a>
 
<?php } ?>

</div>